package hw.test2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyRunnable implements Runnable{

    List<Integer> num  = new ArrayList<>();
    public List<Integer> one  = new ArrayList<>();
    public List<Integer> two  = new ArrayList<>();

    public MyRunnable() {
        Collections.addAll(num,10,5,20,50,100,200,500,800,2,80,300,700);
    }

    @Override
    public void run() {
        while(true){
            //谁抢到锁，谁调用方法，抽奖
            if(sendGift()){
                break;
            }

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private  synchronized boolean sendGift() {
        Collections.shuffle(num);
        if(num.size() > 0){
            System.out.println(Thread.currentThread().getName()+"正在抽奖："+num.get(0));
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if(Thread.currentThread().getName().equals("小明")){
                one.add(num.get(0));
            }else{
                two.add(num.get(0));
            }

            num.remove(0);
            return false;
        }else{

            //统计
            System.out.println(Thread.currentThread().getName()+"抽奖完毕...");
            return true;
        }

    }
}
